// 导入likeModel对象
const likeModel = require('../models/likeModel');
const Jwt = require('../common/jwt');
// 收藏类
class Like {
    // 1. 添加收藏
    async addLike(req, res) {
        let token = req.body.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        if (result.data) {
            let { article_id, article_title } = req.body;
            let user_id = result.data;
            // 执行收藏文章的
            await likeModel.addLike(article_id, user_id, article_title)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '请先登录账号。'
            })
        }
    }
    // 2. 取消收藏
    async removeLike(req, res) {
        let token = req.body.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        if (result.data) {
            // let id = req.body.id;
            let user_id = result.data;
            let article_id = req.body.article_id;
            // 执行删除收藏文章的
            await likeModel.removeLike(article_id, user_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '请先登录账号。'
            })
        }
    }
    // 3. 查看文章是否收藏
    async getDetailLike(req, res) {
        let token = req.body.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        if (result.data) {
            // let id = req.body.id;
            let user_id = result.data;
            let article_id = req.body.article_id;
            // 执行获取收藏
            await likeModel.getDetailLike(article_id, user_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '请先登录账号。'
            })
        }
    }
    // 4. 获取收藏列表
    async getLikes(req, res) {
        let token = req.body.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        if (result.data) {
            // let id = req.body.id;
            let user_id = result.data;
            // 执行获取收藏
            await likeModel.getLikes(user_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '请先登录账号。'
            })
        }
    }
}

// 暴露收藏类的实例
module.exports = new Like();
